package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/*
 *  @author SHCJ
 *  @created 2025/10/6 13:23
 *  @description
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> page(String name, String label, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Role> list = roleMapper.selectPage(name, label);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void delete(Integer id) {
        roleMapper.delete(id);
    }

    @Override
    public void addRole(Role role) {
//        数据校验
        boolean hasNull = BeanUtil.hasNullField(role, "name", "label");
        if(hasNull){
            throw new RuntimeException("参数错误");
        }
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    @Override
    public Object findById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public List<Role> selectAll() {

        return roleMapper.selectAll();
    }
}
